<template>
  <div class="app-container">
    <div class="login-box">
      <el-form ref="form" :model="form" :rules="rules" class="demo-ruleForm" label-width="80px">
        <h3 style="margin-bottom: 20px">登录</h3>
        <el-form-item label="用户名" placeholder="请输入用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" placeholder="请输入密码" prop="password">
          <el-input v-model="form.password" show-password @keyup.enter.native="login"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {login} from "@/api";

export default {
  name: "login",
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [{required: true, message: "请输入用户名", trigger: ["change", "blur"]},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}],
        password: [{required: true, message: "请输入密码", trigger: ["change", "blur"]}],
      }

    }
  },
  methods: {
    async login() {
      try {
        await this.$refs.form.validate()
        await login(this.form).then(res => {
          // console.log("login", {res})
          let {code, data, msg} = res
          if (code === 200) {
            // console.log({data})
            // console.log(data[0])
            this.$message.success(msg)

            window.sessionStorage.setItem('Token', JSON.stringify(data[0]))
            this.$router.push('/')
            //回到登陆前的页面
            // this.$router.go(-1)
          }
        })
      } catch (e) {
        console.log("登录", e)
      }
    }
  }

}
</script>

<style lang="less" scoped>
.app-container {
  width: 100vw;
  height: 100vh;
  background-image: linear-gradient(to bottom, #2c3e50, #98bfe5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-form {
  .el-form-item {
    ::v-deep .el-form-item__label {
      color: #eee;
    }
  }
}
</style>